import { nextTick} from 'vue'

import { createStore } from 'vuex'
import { getUserInfoApi } from '../utils/api.js';

import classic from "./classic/index";
import detail from "./detail/index";
import mani from "./mani/index";

// 创建一个新的 store 实例
const store = createStore({
  state () {
    return {
      userInfo:{//默认的用户信息
        _id:'',
        user_phone:'请输入',
       user_avatar:'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2975996532,2425031589&fm=26&gp=0.jpg',
       user_identity:'请选择',
       user_nickname:'请输入',
       main_management:'请输入',
       user_wechat:'请绑定',
       is_Authentication:false,
       is_Enterprise:false,
      },
      Enterprise_Certification:false,
    }
  },
  
  mutations: {
    //获取用户信息
    getInformations(state,res){//接收提熬过来的数据并根据数据修改仓库中默认的用户信息
      state.userInfo=res.result;
      console.log(state.userInfo);
        switch(res.result.user_identity){
          case 0:res.result.user_identity='其他';
          break;
          case 1:res.result.user_identity='个体种植户';
          break;
          case 2:res.result.user_identity='个体养殖户';
          break;
          case 3:res.result.user_identity='企业种植户';
          break;
          case 4:res.result.user_identity='企业养殖户';
          break;
          case 5:res.result.user_identity='超市';
          break;
          case 6:res.result.user_identity='餐饮店';
          break;
          case 7:res.result.user_identity='酒店';
          break;
          case 8:res.result.user_identity='中间商';
          break;
          case 9:res.result.user_identity='家庭农场';
          break;
          case 10:res.result.user_identity='批发商';
          break;
          case 11:res.result.user_identity='电商';
          break;    
          case 12:res.result.user_identity='食堂';
          break;   
          case 13:res.result.user_identity='贸易公司';
          break;       
        }
        state.userInfo=res.result;
    }

    //
  },
  actions:{
    //获取用户信息
     async getUserInfo({commit}){//通过token获取用户信息并提交给mutations中的getInformations
       if(localStorage.getItem("userToken")){
        const userToken={token:localStorage.getItem("userToken")};
        const res=await getUserInfoApi(userToken);
        commit("getInformations",res);
        console.log(userToken);
       }
    }
  },
  modules: {
    classic,
    detail,
    mani
  },
});

export default store;
